package com.module03.work.demo05;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;

public class PokerGame {

    public static void main(String[] args) throws InterruptedException {

        //创建Map纸牌集合储存集合

        HashMap<Integer, String> pokerMap = new HashMap<Integer, String>();
        //创建花色的集合
        ArrayList<String> pokerColor = new ArrayList<>();
        //创建数字的集合
        ArrayList<String> pokerNumber = new ArrayList<>();

        //分别给花色集合和数字集合赋值
        Collections.addAll(pokerColor, "♥︎", "♠︎", "♣︎", "♦︎");
        Collections.addAll(pokerNumber, "2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");

        //设置Key值起始值cnt
        int cnt = 1;
        //将大小王添加到纸牌集合中
        pokerMap.put(cnt++, "大王");
        pokerMap.put(cnt++, "小王");

        //将其他花色的牌全部添加到纸牌集合中
        for (String number : pokerNumber) {
            for (String color : pokerColor) {
                pokerMap.put(cnt++, color + number);
            }
        }
        System.out.println(pokerMap.toString());
        System.out.println("取出扑克牌，开始洗牌");
        Thread.sleep(3000);
        System.out.println("---------------------------");

        //洗牌

        //将key值放入numberList中，再打乱
        Set<Integer> numberSet = pokerMap.keySet();
        ArrayList<Integer> numberList = new ArrayList<Integer>();
        numberList.addAll(numberSet);
        Collections.shuffle(numberList);
        System.out.println("洗牌结束，开始发牌");
        Thread.sleep(3000);
        System.out.println("---------------------------");

        //发牌
        //创建三个玩家集合和一个底牌集合用于储存打乱后numberList编号的结果
        ArrayList<Integer> numList1 = new ArrayList<>();
        ArrayList<Integer> numList2 = new ArrayList<>();
        ArrayList<Integer> numList3 = new ArrayList<>();
        ArrayList<Integer> hiddenNumList = new ArrayList<>();


        //使用for循环发牌

        for (int i = 0; i < numberList.size() / 3; i++) {
            if (i == numberList.size() / 3 - 1) {
                hiddenNumList.add(numberList.get(3 * i));
                hiddenNumList.add(numberList.get(3 * i + 1));
                hiddenNumList.add(numberList.get(3 * i + 2));
            } else {
                numList1.add(numberList.get(3 * i));
                numList2.add(numberList.get(3 * i + 1));
                numList3.add(numberList.get(3 * i + 2));
            }
        }

        //排序
        Collections.sort(numList1);
        Collections.sort(numList2);
        Collections.sort(numList3);

        //创建四个集合查看牌面结果
        ArrayList<String> pokerPlayerList1 = new ArrayList<>();
        ArrayList<String> pokerPlayerList2 = new ArrayList<>();
        ArrayList<String> pokerPlayerList3 = new ArrayList<>();
        ArrayList<String> hiddenPokerList = new ArrayList<>();
        for (Integer i : numList1) {
            String card = pokerMap.get(i);
            pokerPlayerList1.add(card);
        }
        for (Integer i : numList2) {
            String card = pokerMap.get(i);
            pokerPlayerList2.add(card);
        }
        for (Integer i : numList3) {
            String card = pokerMap.get(i);
            pokerPlayerList3.add(card);
        }
        for (Integer i : hiddenNumList) {
            String card = pokerMap.get(i);
            hiddenPokerList.add(card);
        }

        //查看
        System.out.println("我：" + pokerPlayerList1);
        System.out.println("马云：" + pokerPlayerList2);
        System.out.println("马化腾：" + pokerPlayerList3);
        System.out.println("底牌：" + hiddenPokerList);

    }
}
